// Service\userInfoService.js
import wxBaaS from '../sdk-wechat.3.23.5';

// 获取用户信息
export async function getUserInfo(userId) {
  try {
    const Users = new wxBaaS.TableObject('users');
    const query = new wxBaaS.Query();
    query.compare('id', '=', userId);
    const response = await Users.setQuery(query).find();
    if (response.data.objects.length > 0) {
      return response.data.objects[0];
    } else {
      throw new Error('用户信息未找到');
    }
  } catch (error) {
    console.error('获取用户信息失败:', error);
    throw error;
  }
}

// 更新用户信息
export async function updateUserInfo(userId, updateData) {
  try {
    const Users = new wxBaaS.TableObject('users');
    const user = Users.getWithoutData(userId);
    

    // 只更新 nickname 和 avatar_url
    if (updateData.nickname) {
      user.set('nickname', updateData.nickname);
    }
    if (updateData.avatarUrl) {
      user.set('avatar_url', updateData.avatarUrl);
    }

    await user.update();
    return true;
  } catch (error) {
    console.error('更新用户信息失败:', error);
    throw error;
  }
}


